﻿using ERPWinFormWork.Product.Model;
using ERPWinFormWork.Product.ProductErrorCode;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static ERPWinFormWork.Produce.CreateProduction_Plan;

namespace ERPWinFormWork.Product
{
    public partial class StockinAdd : Form
    {
        private ApiClient _apiClient;
        public StockinAdd()
        {
            InitializeComponent();
            this._apiClient = _apiClient;
            _apiClient = new ApiClient(AppConfig.ApiBaseUrl);
            GetProductSelect();
            GetProductSelect1();
            GetProductSelect2();
            GetProductSelect3();
            GetProductSelect4();
            GetProductSelect5();
        }

        /// <summary>
        /// 产品入库的实体类
        /// </summary>
        public class StockinAddModel
        {
            /// <summary>
            /// 入库单名称
            /// </summary>
            public string Stockin_Name { get; set; }

            /// <summary>
            /// 入库单编号
            /// </summary>
            public string Stockin_Number { get; set; }

            /// <summary>
            /// 入库日期
            /// </summary>
            public DateTime Stockin_Time { get; set; }

            ///// <summary>
            ///// 工单名称
            ///// </summary>
            //public string WorkOrder_Name { get; set; }

            ///// <summary>
            ///// 工单编号
            ///// </summary>
            //public string WorkOrder_Number { get; set; }

            /// <summary>
            /// 工单id
            /// </summary>
            public int WorkOrderId { get; set; }

            /// <summary>
            /// 备注
            /// </summary>
            public string Stockin_Desc { get; set; }

            /// <summary>
            /// 产品主键(关联)
            /// </summary>
            public int Product_Id { get; set; }

            /// <summary>
            /// 产品类型(关联)
            /// </summary>
            public int ProductType_Id { get; set; }

            /// <summary>
            /// 单位
            /// </summary>
            public string Stockin_Unit { get; set; }

            /// <summary>
            /// 批次号
            /// </summary>
            public string Batch { get; set; }

            /// <summary>
            /// 入库数量
            /// </summary>
            public int Stockin_Num { get; set; }

            /// <summary>
            /// 生产日期
            /// </summary>
            public DateTime CreateTime { get; set; }

            /// <summary>
            /// 到期日期
            /// </summary>
            public DateTime OpenTime { get; set; }

            /// <summary>
            /// 仓库(关联)
            /// </summary>
            public int Warehouse_Id { get; set; }

            /// <summary>
            /// 库位
            /// </summary>
            public int AreaId { get; set; }

            /// <summary>
            /// 库区
            /// </summary>
            public int StorageId { get; set; }

            /// <summary>
            /// 状态
            /// </summary>
            public int Stockin_State { get; set; }

        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {

        }

        /// <summary>
        /// 新增按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void skinButton1_Click(object sender, EventArgs e)
        {
            // 检查_apiClient是否初始化
            if (_apiClient == null)
            {
                MessageBox.Show("API客户端未初始化", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            // 构建请求实体对象，将窗体控件值赋值给实体属性
            var requestModel = new StockinAddModel
            { 
                Stockin_Number = txtDiscount_Number.Text.Trim(),
                Stockin_Name = skin_Name.Text.Trim(),
                Stockin_Time = date_creaTime.Value,
                WorkOrderId = int.TryParse(comword.SelectedValue.ToString(), out int Product) ? Product : 0,
                Stockin_Desc = skin_Desc.Text.Trim(),
                //Specification = txtSpecification.Text.Trim(),com_cang1
                Product_Id = int.TryParse(comproductname.SelectedValue.ToString(), out int Product1) ? Product1 : 0,
                ProductType_Id = int.TryParse(com_Type.SelectedValue.ToString(), out int Product2) ? Product2 : 0,
                Stockin_Unit = skin_Dan.Text.Trim(),
                Batch = skinTextBox9.Text.Trim(),
                Stockin_Num = int.TryParse(skin_RNum.Text.Trim(), out int quantity) ? quantity : 0,
                CreateTime = dateTime1.Value,
                OpenTime = dateTime2.Value,
                Warehouse_Id = int.TryParse(com_cang1.SelectedValue.ToString(), out int Product3) ? Product3 : 0,
                AreaId = int.TryParse(com_cang2.SelectedValue.ToString(), out int Product4) ? Product4 : 0,
                StorageId = int.TryParse(com_cang3.SelectedValue.ToString(), out int Product5) ? Product5 : 0,
                Stockin_State = int.TryParse(skin_Fu.Text.Trim(), out int quantitys) ? quantitys : 0,
                
            };
            var response = await _apiClient.PostAsync<StockinAddModel, ApiResult<string>>(
            endpoint: "Product/AddStockin",
            data: requestModel
            );
            if (response.code == 200)
            {
                // 这里可根据后端返回的结果做相应处理，比如简单提示成功
                MessageBox.Show("产品入库添加成功，后端返回：", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                // 可选择清空窗体控件内容，准备下次添加

            }
            else
            {
                MessageBox.Show("调用接口失败，状态码：", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }




        /// <summary>
        /// 产品查询
        /// </summary>
        private async void GetProductSelect()
        {
            var response = await _apiClient.GetAsync<ApiResult<List<Product_Model>>>(
                    endpoint: "Product/ShowProduct"
                );
            if (response != null)
            {
                comproductname.DataSource = response.data;
                comproductname.DisplayMember = "product_Name";//指定显示的字段
                comproductname.ValueMember = "id";//绑定id属性
            }
        }

        /// <summary>
        /// 产品类型查询
        /// </summary>
        private async void GetProductSelect1()
        {
            var response = await _apiClient.GetAsync<ApiResult<List<ProductType_Model>>>(
                    endpoint: "Product/ProductShow"
                );
            if (response != null)
            {
                com_Type.DataSource = response.data;
                com_Type.DisplayMember = "productName";//指定显示的字段
                com_Type.ValueMember = "id";//绑定id属性
            }
        }

        /// <summary>
        /// 工单查询
        /// </summary>
        private async void GetProductSelect2()
        {
            var response = await _apiClient.GetAsync<ApiResult<List<Work_Task>>>(
                    endpoint: "Product/Showworkword"
                );
            if (response != null)
            {
                comword.DataSource = response.data;
                comword.DisplayMember = "task";//指定显示的字段
                comword.ValueMember = "id";//绑定id属性
            }
        }

        /// <summary>
        /// 库位查询
        /// </summary>
        private async void GetProductSelect3()
        {
            var response = await _apiClient.GetAsync<ApiResult<List<Area_Model>>>(
                    endpoint: "Product/AreaShow"
                );
            if (response != null)
            {
                com_cang2.DataSource = response.data;
                com_cang2.DisplayMember = "warehouse_area";//指定显示的字段
                com_cang2.ValueMember = "id";//绑定id属性
            }
        }

        /// <summary>
        /// 库区查询
        /// </summary>
        private async void GetProductSelect4()
        {
            var response = await _apiClient.GetAsync<ApiResult<List<Storage_Model>>>(
                    endpoint: "Product/StorageShow"
                );
            if (response != null)
            {
                com_cang3.DataSource = response.data;
                com_cang3.DisplayMember = "storage_location";//指定显示的字段
                com_cang3.ValueMember = "id";//绑定id属性
            }
        }

        /// <summary>
        /// 仓库查询
        /// </summary>
        private async void GetProductSelect5()
        {
            var response = await _apiClient.GetAsync<ApiResult<List<Warehouse_Model>>>(
                    endpoint: "Product/WareHouseShow"
                );
            if (response != null)
            {
                com_cang1.DataSource = response.data;
                com_cang1.DisplayMember = "warehouse_Name";//指定显示的字段
                com_cang1.ValueMember = "id";//绑定id属性
            }
        }

        /// <summary>
        /// 工单名称/编号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comword_SelectedIndexChanged(object sender, EventArgs e)
        {
            var selectedProduct = comword.SelectedItem as Work_Task;
            if (selectedProduct != null)
            {
                skin_WNumber.Text = selectedProduct.Task_Number ?? "";

            }
            else
            {
                skin_WNumber.Text = "";
            }
        }

        /// <summary>
        /// 产品名称
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comproductname_SelectedIndexChanged(object sender, EventArgs e)
        {
            var selectedProduct = comproductname.SelectedItem as Product_Model;
            if (selectedProduct != null)
            {
                skin_PNumber.Text = selectedProduct.Product_Number ?? "";
                skin_Gui.Text = selectedProduct.Specification ?? "";

            }
            else
            {
                skin_PNumber.Text = "";
                skin_Gui.Text = "";
            }
        }



    }
}
